package mornd.test1;

import java.lang.reflect.Proxy;

/**
 * @author: mornd
 * @dateTime: 2023/5/5 - 19:06
 * jdk 动态代理有限制，只能针对接口代理，cglib 没有这些限制
 */
public class Demo {
    public static void main(String[] ar) throws Exception {
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");

        A a = new B();
        A proxyA = (A) Proxy.newProxyInstance(Demo.class.getClassLoader(),
                a.getClass().getInterfaces(),
                (proxy, method, args) -> {
                    System.out.println("before");
                    Object r = method.invoke(a, args);
                    System.out.println("after");
                    return r;
                });

        System.out.println(proxyA.getClass());

        proxyA.func("");


        //System.in.read();
    }

    static interface A {
        int func(String name);
    }

    static class B implements A {

        public int func(String name) {
            System.out.println("B func()-" + name);
            return 1;
        }
    }
}
